local jieyin = fk.CreateSkill {
  name = "ym_water_lieshijiexiehou1__jieyin&",
  dynamic_desc = function (self, player, lang)
    local mark = player:getTableMark("ym_water_lieshijiexiehou_jieyin")
    local str = "出牌阶段限一次，你可以弃置两张手牌，令你和一名已受伤的"
    if not table.contains(mark, "人称") then
      str = str .. "其他"
    end
    if not table.contains(mark, "性别") then
      str = str .. "男性"
    end
    str = str .. "角色各回复1点体力。"
    return str
  end,
}
Fk:loadTranslationTable{
  ["ym_water_lieshijiexiehou1__jieyin&"] = "结姻",
  [":ym_water_lieshijiexiehou1__jieyin&"] = "出牌阶段限一次，你可以弃置两张手牌，令你和一名已受伤的其他男性角色各回复1点体力。",
}

local banskills = {"ym_water_lieshijiexiehou1__jieyin&","ym_water_lieshijiexiehou2__jieyin&","ym_water_lieshijiexiehou3__jieyin&","ym_water_lieshijiexiehou"}

jieyin:addEffect("active", {
  anim_type = "support",
  prompt = function (self, player, selected_cards, selected_targets)
    local mark = player:getTableMark("ym_water_lieshijiexiehou_jieyin")
    local str = "发动 结姻，弃置两张手牌并选择一名已受伤的"
    if not table.contains(mark, "人称") then
      str = str .. "其他"
    end
    if not table.contains(mark, "性别") then
      str = str .. "男性"
    end
    str = str .. "角色，你与其各回复1点体力"
    return str
  end,
  max_phase_use_time = 1,
  card_filter = function(self, player, to_select, selected)
    return #selected < 2 and table.contains(player:getCardIds("h"), to_select) and not player:prohibitDiscard(to_select)
  end,
  target_filter = function(self, player, to_select, selected)
    local mark = player:getTableMark("ym_water_lieshijiexiehou_jieyin")
    return #selected == 0 and to_select:isWounded() and (to_select:isMale() or table.contains(mark, "性别")) and (to_select ~= player or table.contains(mark, "人称"))
  end,
  target_num = 1,
  card_num = 2,
  on_use = function(self, room, effect)
    local from = effect.from
    local target = effect.tos[1]
    room:throwCard(effect.cards, jieyin.name, from, from)
    if target:isAlive() and target:isWounded() then
      room:recover({
        who = target,
        num = 1,
        recoverBy = from,
        skillName = jieyin.name,
      })
    end
    if from:isAlive() and from:isWounded() then
      room:recover({
        who = from,
        num = 1,
        recoverBy = from,
        skillName = jieyin.name,
      })
    end
    if from ~= target and from:hasSkill("ym_water__lieshijiexiehou") then
      local skills = {}
      local skillKeys = {}
      for index, s in ipairs(from.player_skills) do
        if s:isPlayerSkill(from) and s.visible and s.name then
          local effect_spec_list = s.skeleton.effect_spec_list
          for _, effect_spec in ipairs(effect_spec_list) do
            table.insertIfNeed(skillKeys,effect_spec[1])
          end
        end
      end
      --找时机不同的技能，或出牌阶段限次的技能
      for index, s in ipairs(target.player_skills) do
        if s:isPlayerSkill(target) and s.visible and s.name and not table.contains(banskills,s.name) then
          local bool = true
          local effect_spec_list = s.skeleton.effect_spec_list
          for _, effect_spec in ipairs(effect_spec_list) do
            if table.contains(skillKeys,effect_spec[1]) and bool then
              bool = false
            end
          end
          if not from:hasSkill(s.name) and (bool or string.find(Fk:translate(":"..s.name, "zh_CN"),"出牌阶段限") or string.find(Fk:translate(":"..s.name, "zh_CN"),"出牌阶段各限")) then
            table.insert(skills,s.name)
          end
        end
      end
      if #skills > 0 and from:hasSkill(jieyin.name,true) then
        table.insert(skills,"Cancel")
        local choice = room:askToChoice(from,{
          choices = skills,
          cancelable = true,
          detailed = true,
          prompt = "#ym_water__lieshijiexiehou::"..target.id,
          skill_name = "ym_water__lieshijiexiehou",
        })
        if choice ~= "Cancel" then
          room:handleAddLoseSkills(from,"-"..jieyin.name.."|"..choice,"ym_water__lieshijiexiehou",false,true)

          local mark = from:getTableMark("ym_water__lieshijiexiehou")
          for index, value in ipairs(mark) do
            if value == jieyin.name then
              mark[index] = choice
              break
            end
          end
          room:setPlayerMark(from,"ym_water__lieshijiexiehou",mark)

          mark = from:getTableMark("ym_water_lieshijiexiehou_jieyin")
          if #mark < 2 then
            local all_choices = {"性别","人称"}
            local choices = table.filter(all_choices,function (element, index, array)
              return not table.contains(mark,element)
            end)
            choice = room:askToChoice(from,{
              choices = choices,
              all_choices = all_choices,
              cancelable = false,
              prompt = "#ym_water__lieshijiexiehou_jieyin",
              skill_name = "ym_water__lieshijiexiehou",
            })
            room:addTableMarkIfNeed(from,"ym_water_lieshijiexiehou_jieyin",choice)
          end
        end
      end
    end
  end,
})

return jieyin